package com.lwc.cfdns.pojo.security;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lwc.cfdns.pojo.entity.SystemAdmin;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 实现UserDetails
 *
 * @author luwc
 * @title SecurityUserDetails
 * @description
 * @time 2023/5/10 17:41:23
 * @version: 1.0
 */
@Data
@NoArgsConstructor
public class SecuritySystemAdminUserDetails implements UserDetails {
    private static final long serialVersionUID = 1L;

    private SystemAdmin systemAdmin;
    // 读取到系统用户的权限
    private List<String> permissions = new ArrayList<>();

    // 存储Security权限信息的集合
    private List<SimpleGrantedAuthority> authoritys = null;

    public SecuritySystemAdminUserDetails(SystemAdmin systemAdmin, List<String> permissions) {
        this.systemAdmin = systemAdmin;
        this.permissions = permissions;
    }

    /**
     * 转化对象
     * 读取到系统用户的权限转化为Security权限信息的集合
     *
     * @param
     * @return Collection<GrantedAuthority>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/16 13:05:35
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (authoritys == null) {
//            // 普通写法
//            for (String permission : permissions) {
//                SimpleGrantedAuthority authority = new SimpleGrantedAuthority(permission);
//                authoritys.add(authority);
//            }

            // 字节流形式
            authoritys = permissions.stream().map(SimpleGrantedAuthority::new)
                    .collect(Collectors.toList());
        }
        return authoritys;
    }

    public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
        authorities = authorities;
    }

    @Override
    @JsonIgnore
    public String getPassword() {
        return systemAdmin.getPassword();
    }

    @Override
    @JsonIgnore
    public String getUsername() {
        return systemAdmin.getUserName();
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return true;
    }
}
